﻿using Artizan.Iot.Hub.Mqtt.Topics;
using MQTTnet;
using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Artizan.Iot.Hub.Mqtt.Server.Extensions
{
    public static class MqttServerExtensions
    {
        /// <summary>
        ////  -----------------------------------------------------------------  
        ///  MQTTnet v3.x :
        ///     MQTTnet v3.x 中的MqttServer 类中的方法PublishAsync() 已被删除，故以下代码不能再使用:
        ///
        ///     await MqttServer.PublishAsync(BrokerEventTopics.NewClientConnected, arg.ClientId);
        ///     ，其源码： https://github.com/dotnet/MQTTnet/blob/release/3.1.x/Source/MQTTnet/Server/MqttServer.cs
        ///     
        ///  ----------------------------------------------------------------- 
        ///   MQTTnet v4.1 ：
        ///     该版本中可以调用：MqttServer.InjectApplicationMessage() 方法注入消息
        ///    
        /// </summary>
        /// <param name="mqttServer"></param>
        /// <param name="mqttApplicationMessage"></param>
        /// <exception cref="ArgumentNullException"></exception>

        public static void PublishByHub(this MqttServer mqttServer, string topic, string payload)
        {
            if (topic == null) throw new ArgumentNullException(nameof(topic));

            mqttServer.PublishByHub(new MqttApplicationMessageBuilder()
                .WithTopic(topic)
                .WithPayload(payload)
                .Build());
        }

        public static void PublishByHub(this MqttServer mqttServer, MqttApplicationMessage mqttApplicationMessage)
        {
            if (mqttServer == null) throw new ArgumentNullException(nameof(mqttServer));
            if (mqttApplicationMessage == null) throw new ArgumentNullException(nameof(mqttApplicationMessage));

            mqttServer.InjectApplicationMessage(new InjectedMqttApplicationMessage(mqttApplicationMessage)
            {
                SenderClientId = MqttServiceConsts.IotHubMqttClientId
            });

        }
    }
}
